package com.jiangli.controller.admin;

import com.jiangli.annotation.Log;
import com.jiangli.constant.JwtClaimsConstant;
import com.jiangli.dto.EmployeeDTO;
import com.jiangli.dto.EmployeeLoginDTO;
import com.jiangli.entity.Employee;
import com.jiangli.properties.JwtProperties;
import com.jiangli.result.Result;
import com.jiangli.service.EmployeeService;
import com.jiangli.utils.JwtUtil;
import com.jiangli.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/admin")
@Slf4j
@Api("员工相关接口")
public class AdminController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 编辑员工信息
     * @param employeeDTO 法
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("编辑管理员信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @Log("员工登入")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("管理员：{}", employeeLoginDTO.getUsername());

       Employee employee = employeeService.login(employeeLoginDTO);
      // Employee employee=new Employee();employee.setId(10L);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }
    /*
     RequestBody 是用于接受json格式的数据
     原生的java不支持 直接接受 json格式
     */
//    @RequestMapping
//    @ApiOperation("新增员工")
//    public Result save(@RequestBody EmployeeDTO employeeDTO) {
//
//        //打印日志
//        //{} 这里是字符串拼接
//        log.info("新增加员工：{}",employeeDTO);
//        employeeService.save(employeeDTO);
//        return Result.success();
//
//    }
////    @GetMapping("/page")
//    @ApiOperation("员工分页查询")
//    public Result page(EmployeePageQueryDTO employeePageQueryDTO) {
//        log.info("员工分页查询：参数为{}",employeePageQueryDTO);
//        PageResult pageResult=employeeService.pageQuery(employeePageQueryDTO);
//        return Result.success(pageResult);
//
//
//    }
//
//    /**
//     * 启用禁用员工账号
//     * 通过路劲传参
//     * @param status
//     * @param id
//     * @return
//     */
//    @PostMapping("/status/{status}")
//    @ApiOperation("启用禁用员工账号")
//    public Result startOrStop(@PathVariable Integer status,Long id) {
//        log.info("启用禁用员工账号：{}{}",status,id);
//        employeeService.startOrStop(status,id);
//        return Result.success();
//    }
//
//    /**
//     * 退出
//     *
//     * @return
//     */
//    @PostMapping("/logout")
//    public Result<String> logout() {
//        return Result.success();
//    }
////
////    /**
////     * 根据id查找用户
////     * @param id
////     * @return
//     */
//    @GetMapping("/{id}")
//    @ApiOperation("根据id查找用户")
//    public Result<Employee> getById(@PathVariable Long id) {
//        log.info("根据id查找用户");
//       Employee employee= employeeService.getById(id);
//        return Result.success(employee);
//    }



}
